
!(function (t, g) {
    if (t) {
        t.__proto__.$ = g;
    }
    else if ("object" == typeof module && "object" == typeof module.exports) {
        module.exports = g;
    }
})(this || undefined, (function () {
    var ID_VERSION = "ID.VERSION." + new Date().getMilliseconds() + "" + parseInt(Math.random() * 10000);
    var deep_copy = function (obj) {
        if (obj === null || typeof obj !== "object") {
            return obj;
        }
        var copy = Array.isArray(obj) ? [] : {};
        for (var key in obj) {
            if (obj.hasOwnProperty(key)) {
                copy[key] = deep_copy(obj[key]);
            }
        }
        return copy;
    }
    var object_contains = function (obj, key) {
        if (!obj || !key) {
            return false;
        }
        else {
            if (is_object(obj)) {
                return obj[key] !== undefined;
            } else {
                return false;
            }
        }
    }
    var is_document = function (el) {
        return el && (el instanceof Element || el instanceof Document || el instanceof DocumentFragment);
    }
    var set_props = function (el, props, call) {
        if (!el) {
            return;
        }
        else if (call && typeof call === "function") {
            for (var key in props) {
                if (Object.prototype.hasOwnProperty.call(props, key)) {
                    var element = props[key];
                    call(el, key, element);
                }
            }
        }
    }
    var is_json = function (o) {
        try {
            JSON.stringify(o);
            return true;
        } catch (err) {
            return false;
        }
    }
    var is_object = function (obj, check_blank) {
        if (undefined === check_blank) { check_blank = false }
        try {
            return obj && is_json(obj) && typeof obj === "object" && Reflect.ownKeys(obj).length >= (check_blank ? 1 : 0);
        } catch (err) {
            if (err.number === -2146823279) {
                var t = false;
                for (var o in obj) {
                    t = true;
                    break;
                }
                return t;
            } else {
                return false;
            }
        }
    }
    var str_is_empty = function (str) {
        if (!str || typeof str !== "string") {
            return true;
        }
        return str.trim().length === 0;
    }
    var array_not_empty = function (arr) {
        return (arr && arr.length > 0);
    }
    var is_array = function (arr, check_blank) {
        if (undefined === check_blank) { check_blank = false }
        return arr && typeof arr === "object" && (check_blank ? array_not_empty(arr) : arr.length !== undefined)
    }
    var is_function = function (f) {
        return f && typeof f === "function";
    }
    var get_style = function (el, prop) {
        var _el = undefined;
        if (!el) {
            return prop;
        }
        else if (!is_document(el)) {
            _el = el.$el;
        } else {
            _el = el;
        }
        if (!prop) {
            return _el.getBoundingClientRect();
        } else if (prop && typeof prop === "string") {
            _el.getBoundingClientRect()[prop];
        }
    }
    var has_class = function (el, class_name) {
        return el.classList.contains(class_name);
    }
    var is_window = function (e) {
        return e && e === window;
    }
    var root = null;
    var ctx = null;
    function r(obj, _key) {
        var _ctx = ctx;
        if (!_ctx) { return; }
        var reg = "\\$\\\(" + _key + "\\)";
        var r = new RegExp(reg, "g");
        _ctx = _ctx.replaceAll(r, obj[_key]);
        console.log(_ctx);
        root.text(_ctx);
    }

    /**声明响应式 */
    var to_ref = function (obj) {
        if (Object.keys(obj).length > 0) {
            return new Proxy(obj, {
                set(t, k, v) {
                    t[k] = v;
                    for (var k1 in t) {
                        // console.log(k1, t);
                        r(t, k1);
                    }
                },
                get(k, t, r) {
                    return k[t];
                }
            });
        }
    }
    /**解除响应式 */
    var to_raw = function (target, handler) {
        Proxy.revocable(target, handler);
    }
    var parseHtml = function () {

    }
    var f = function (o, parent) {
        if (parent) {
            root = parent;
            ctx = root.text();
        }
        var _el = undefined;
        if (o === undefined) {
            _el = document.createDocumentFragment();
        } else if (is_document(o)) {
            _el = o;
        } else if (!str_is_empty(o)) {
            var CLASS = /^[.]/g.test(o);
            var ID = /^[#]/g.test(o);
            if (CLASS || ID) {
                var els = document.querySelectorAll(o),
                    len = els.length;
                _el = len === 1 ? els[0] : els;
            } else if (/<[a-z]+[1-6]?\b[^>]*>(.*?)|<\/[a-z]+[1-6]?>/g.test(o)) {
                try {
                    var parse = new DOMParser().parseFromString(o, "text/html");
                    _el = parse.body.firstChild;
                } catch (err) {
                    console.error("dom 解析失败", err);
                }
            }
            else {
                var els = document.querySelectorAll(o);
                if (els) {
                    var len = els.length;
                    _el = len === 1 ? els[0] : els;
                }
            }
        }
        else {
            _el = o;
        }
        var g = {
            $el: _el,
            events: [],
            ready: function (call) {
                if (is_function(call)) {
                    if (is_window(this.$el)) {
                        this.$el.onload = function () {
                            call(this.$el);
                        }
                    } else if (is_document(this.$el)) {
                        try {
                            this.$el.addEventListener("DOMContentLoaded", function () {
                                call(this.$el);
                            });
                        } catch (err) {
                            void (err);
                        }
                    }
                }
                return undefined;
            },
            add_attr: function (node, k, v, is_class, id) {
                if (is_class === undefined) {
                    is_class = false;
                }
                if (id !== ID_VERSION) {
                    throw new Error();
                }
                if (is_document(node)) {
                    if (is_class) {
                        node.classList.add(v);
                    } else {
                        node.setAttribute(k, v);
                    }
                }
                return undefined;
            },
            attr: function (prop) {
                var t = this;
                if (is_object(prop, true)) {
                    set_props(this.$el, prop, function (e, k, v) {
                        var is_class = k === "class";
                        if (is_array(e, true)) {
                            e.forEach(function (item) {
                                t.add_attr(item, k, v, is_class, ID_VERSION);
                            });
                        } else {
                            t.add_attr(e, k, v, is_class, ID_VERSION)
                        }
                    });
                }
                return this;
            },
            className: function (id, name, add) {
                if (ID_VERSION !== id) {
                    throw new Error("禁止访问");
                }
                if (!name || typeof name !== "string" || name === "") {
                    return;
                }
                if (is_array(this.$el, true)) {
                    this.$el.forEach(function (item) {
                        if (add) {
                            item.classList.add(name);
                        } else {
                            item.classList.remove(name);
                        }
                    })
                } else if (is_document(this.$el)) {
                    if (add) {
                        this.$el.classList.add(name);
                    } else {
                        this.$el.classList.remove(name);
                    }
                }
            },
            removeClass: function (name) {
                this.className(ID_VERSION, name, false);
                return this;
            },
            addClass: function (name) {
                this.className(ID_VERSION, name, true);
                return this;
            },
            delay: function (call, time) {
                if (time < 0) {
                    call();
                    return this;
                }
                var _t = this.$el;
                var node_list = [];
                if (is_document(_t)) {
                    node_list.push(_t);
                } else if (is_array(_t)) {
                    node_list = _t;
                }
                if (is_function(call)) {
                    var t = setTimeout(function () {
                        clearTimeout(t);
                        node_list.forEach(function (item) {
                            call.call(item);
                        });
                        t = null;
                    }, time);
                }
                return this;
            },
            css: function (prop) {
                if (is_object(prop, true)) {
                    set_props(this.$el, prop, function (e, k, v) {
                        if (is_array(e, true)) {
                            e.forEach(function (item) {
                                item.style[k] = v;
                            });
                        } else if (is_document(e)) {
                            e.style[k] = v;
                        }
                    });
                } else {
                    void (0);
                }
                return this;
            },
            add: function (node) {
                var _el = this.$el;
                var tag = is_array(_el, true);
                function _push(element, insert_content) {
                    if (!insert_content) {
                        return;
                    }
                    if (tag) {
                        var i = 0,
                            len = element.length;
                        for (; i < len; i++) {
                            element[i].appendChild(insert_content);
                        }
                    } else if (is_document(_el)) {
                        element.appendChild(insert_content);
                    }
                }
                if (is_array(node, true)) {
                    var vm = $();
                    node.forEach(function (e) {
                        vm.add(e);
                    });
                    _push(this.$el, vm.$el);
                }
                else if (is_document(node)) {
                    _push(this.$el, node);
                } else if (is_object(node) && node.$el !== undefined) {
                    _push(this.$el, node.$el);
                }
                else {
                    void (0);
                }
                return this;
            },
            remove: function () {
                var this_el = this.$el;
                if (is_array(this_el, false)) {
                    if (this_el.length > 0) {
                        var parent = this_el[0].parentNode;
                        console.log(parent);
                        return;
                        if (parent) {
                            this_el.forEach(function (item) {
                                if (item) {
                                    parent.removeChild(item);
                                }
                            });
                        }
                    }
                } else {
                    this_el.parentNode.removeChild(this_el);
                }
                return this;
            },
            clone: function (copy_child) {
                if (undefined === copy_child) {
                    copy_child = true;
                }
                if (is_array(this.$el, true)) {
                    var arr = []; this.$el.forEach(function (e) {
                        arr.push(e.cloneNode(copy_child));
                    });
                    return arr;
                } else {
                    return this.$el.cloneNode(copy_child);
                }
            },
            size: function () {
                if (!this.$el) {
                    return 0;
                } else if (is_array(this.$el)) {
                    return this.$el.length;
                } else {
                    return 1;
                }
            },
            children: function (name) {
                var list = [];
                var first_floor_child = this.$el.children;
                if (!name) {
                    this.$el = first_floor_child;
                    return this;
                }
                var _find = function (node) {
                    if (!node) {
                        return this;
                    }
                    var len = node.length;
                    var i = 0;
                    for (; i < len; ++i) {
                        var every_node = node[i];
                        try {
                            if (array_not_empty(every_node.children, true)) {
                                _find(every_node.children);
                            }
                            var _pirex = name.replace(".", "");
                            if (every_node.classList.contains(_pirex)) {
                                list.push(every_node);
                            }
                        } catch (err) {
                            console.error(err);
                            void (err);
                        }
                    }
                }
                _find(first_floor_child);
                this.$el = list;
                return this;
            },
            each: function (call) {
                if (call && typeof call === "function") {
                    if (this.$el && this.$el.length) {
                        for (var key in this.$el) {
                            call(key, this.$el[key]);
                        }
                    } else {
                        console.error("当前集合不可迭代", this.$el);
                    }
                }
                return this;
            },
            toggleClass: function (name) {
                var t = this;
                var temp_list = [];
                if (is_array(t.$el, true)) {
                    t.$el.forEach(function (item) {
                        temp_list.push(item);
                    });
                } else if (is_document(t.$el)) {
                    temp_list.push(t.$el);
                } else {
                    temp_list = null;
                }
                if (temp_list && temp_list.length > 0) {
                    temp_list.forEach(function (item) {
                        if (has_class(item, name)) {
                            t.removeClass(name);
                        } else {
                            t.addClass(name);
                        }
                    })
                }
                return this;
            },
            text: function (val) {
                var str_empty = str_is_empty(val);
                if (is_document(this.$el)) {
                    if (str_empty) {
                        return this.$el.innerText;
                    } else {
                        this.$el.innerText = val;
                        return this;
                    }
                } else if (is_array(this.$el, true)) {
                    var contentText = "";
                    this.$el.forEach(function (item) {
                        if (str_empty) {
                            contentText = contentText + item.innerText;
                        } else {
                            item.innerText = val;
                        }
                    });
                    if (str_empty) {
                        return contentText;
                    } else {
                        return this;
                    }
                }
            },
            get: function (idx) {
                if (undefined === idx) { idx = 0; }
                if (this.$el && this.$el.length) {
                    this.$el = this.$el[idx];
                }
                return this;
            },
            on: function (event, func) {
                if (!event || !func || typeof func !== "function") {
                    console.error("params error");
                    return;
                }
                var t = this;
                var el = t.$el;
                t.bind_event(ID_VERSION, el, function (e) {
                    e.addEventListener(event, function (e) {
                        func.call(el, e);
                    }, {
                        passive: false,
                        capture: true
                    });
                    t.events.push(func);
                });
                return this;
            },
            [Symbol('bind_event')]: function () {

            },
            bind_event: function (number, el, call) {
                if (number !== ID_VERSION) {
                    throw new Error("禁止访问");
                }
                else if (is_document(el) && is_function(call)) {
                    call(el);
                } else if (is_array(el, true)) {
                    for (var ev in el) {
                        var _el = el[ev];
                        if (is_document(_el) && is_function(call)) {
                            call(el[ev]);
                        }
                    }
                }
            },
            off: function (event, fun) {
                var _events = this.events;
                if (_events.length === 0) {
                    return;
                }
                this.bind_event(ID_VERSION, this.$el, function (el) {// 需要解决一下解绑后的句柄
                    _events.forEach(function (func, index) {
                        el.removeEventListener(event, fun, true);
                        _events.splice(index, 1);
                    })
                });
                return this;
            },
            hover: function (func, func2) {
                if (is_function(func)) {
                    this.on("mouseenter", func);
                }
                if (is_function(func2)) {
                    this.on("mouseleave", func2);
                }
                return this;
            },
            press: function (func, func2) {
                if (is_function(func)) {
                    this.on("mousedown", func);
                }
                if (is_function(func2)) {
                    this.on("mouseup", func2);
                }
            },
            click: function (func, func2) {
                if (is_function(func)) {
                    this.on("click", func);
                }
            },
            value: function () {
                var val = "";
                if (is_array(this.$el, true)) {
                    this.$el.forEach(function (item) {
                        if (item.tagName === "INPUT") {
                            val = val + item.value;
                        } else {
                            val = val + item.innerText;
                        }
                    })
                } else if (is_document(this.$el)) {
                    if (this.$el.tagName === "INPUT") {
                        val = this.$el.value;
                    } else {
                        val = this.$el.innerTextc
                    }
                }
                return val;
            }
        };
        g.__proto__.every = function (arr, call) {
            if (!arr || !call) {
                return;
            }
            if (is_array(arr, true) && is_function(call)) {
                var len = arr.length;
                var index = 0;
                for (; index < len;) {
                    call.apply(null, [arr[index], index]);
                    index++;
                }
            }
            return void (arr);
        }
        return g;
    }
    return f;
})());

